// home/site/site-details.js
import api from '../../utils/api.js';
import network from '../../utils/network.js';
import notice from '../../utils/notice.js';
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        statusBarHeight: 0,
        list: ['', ''],
        visible: false,
        details: {},
        couponList: [],
        calendarShow: false,
        timerValue: {},
        isShare: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        notice.addNotice("UserInfo", this.refreshUserInfo, this);
        notice.addNotice("GetTimer", this.getTimer, this);
        this.windowInfo = wx.getWindowInfo();
        this.setData({
            statusBarHeight: this.windowInfo.statusBarHeight
        })

        network.getJson(api.getSite, {
            id: options.id
        }).then(res => {
            res.data.images = res.data.images.split(',');
            // res.data.details = res.data.details.replace(/<img/gi, '<img style="max-width:100%;height:auto;margin:0 auto;display:block"')
            let timerAmount = [res.data.amFees, res.data.pmFees, res.data.atFees];
            this.setData({
                details: res.data,
                timerValue: {
                    timerAmount,
                    list: [],
                    siteId: res.data.id
                }
            })
        })

        network.getJson(api.activeCoupon, {
            siteId: options.id,
            excloudPayCouponType: 2
        }).then(res => {
            this.setData({
                couponList: res.data.list
            })
        })

        if (options.isShare) {
            this.setData({
                isShare: true
            })
        }

        wx.showShareMenu({
            withShareTicket: true,
            menus: ['shareAppMessage', 'shareTimeline']
        })
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        notice.removeNotice("UserInfo", this);
        notice.removeNotice("GetTimer", this);
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {
        let user = app.globalData.userInfo;
        return {
            title: this.data.details.name,
            imageUrl: this.data.details.images[0],
            path: `/home/site/site-details?id=${this.data.details.id}&parentId=${user.id}` + '&isShare=true'
        }
    },

    onShareTimeline() {
        let user = app.globalData.userInfo;
        return {
            title: this.data.details.name,
            imageUrl: this.data.details.images[0],
            path: `/home/site/site-details?id=${this.data.details.id}&parentId=${user.id}` + '&isShare=true'
        }
    },

    refreshUserInfo() {
        let userInfo = app.globalData.userInfo;
        this.setData({
            userInfo
        })
    },

    homeTap() {
        wx.reLaunch({
            url: '/pages/home/home',
        })
    },

    backTap() {
        wx.navigateBack();
    },

    callPhone(e) {
        wx.makePhoneCall({
            phoneNumber: this.data.details.phone
        })
    },

    navTap(e) {
        wx.openLocation({
            name: this.data.details.name,
            latitude: this.data.details.lat,
            longitude: this.data.details.lng
        })
    },

    goPay() {
        if (app.globalData.userInfo.mobile) {
            this.setData({
                calendarShow: true
            })
        } else {
            wx.navigateTo({
                url: '/myself/login/login',
            })
        }
    },

    submit(e) {
        if (e.detail.length > 0) {
            wx.navigateTo({
                url: '/home/pay/pay?site=' + encodeURIComponent(JSON.stringify(this.data.details)) + '&timer=' + encodeURIComponent(JSON.stringify(e.detail))
            })
        } else {
            wx.showToast({
                icon: 'none',
                title: '请选择活动时间'
            })
        }
    },

    startShared() {
        // if (app.globalData.userInfo.mobile) {
        //     this.setData({
        //         visible: true
        //     });
        // } else {
        //     wx.navigateTo({
        //         url: '/myself/login/login',
        //     })
        // }
    },

    openInfo(){
        this.setData({
            visible: true
        });
    }, 

    onVisibleChange(e) {
        this.setData({
            visible: e.detail.visible,
        });
    },
    getTimer(obj) {
        this.setData({
            [`timerValue.list`]: obj
        })
    },
    // goPromotion() {
    //   wx.navigateTo({
    //     url: '/home/promotion/promotion',
    //   })
    // }
    goCoupon(e) {
        let coupon = e.currentTarget.dataset.item
        console.log(coupon)
        wx.navigateTo({
            url: '/site/coupon/coupon?coupon=' + encodeURIComponent(JSON.stringify(coupon)) + '&site=' + encodeURIComponent(JSON.stringify(this.data.details))
        })
    }
})